<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-input clearable @clear="searchGoods" v-model="goodsQuery.query" style="width: 300px" placeholder="请输入内容" @keyup.enter.native="searchGoods">
                    <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
                </el-input>
                <el-button type="primary" style="margin-left: 30px" @click="goGoodsAdd">添加商品</el-button>
            </el-row>
            <el-table :data="tableData" stripe border>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name" width="600px"></el-table-column>
                <el-table-column label="商品价格（元）" prop="goods_price" width="120px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
                <el-table-column label="创建时间" prop="add_time">
                    <template slot-scope="scope">
                        {{scope.row.add_time|dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作"width="130px">
                    <template slot-scope="scope">
                        <el-button size="mini" icon="el-icon-edit" type="primary" @click="editGoods(scope.row)"></el-button>
                        <el-button size="mini" icon="el-icon-delete" type="danger" @click="deleteGoods(scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="goodsQuery.pagenum"
                    :page-sizes="[3, 5, 10, 15]"
                    :page-size="goodsQuery.pagesize"
                    layout="total,sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "list",
        data(){
            return{
                //表格数据源
                tableData:[],
                goodsQuery:{
                    query:'',
                    pagenum:1,
                    pagesize:10
                },
                total:0
            }
        },
        methods:{
            getGoodsList(){
                this.$http.get('goods',{params:this.goodsQuery}).then((res)=>{
                    if (res.data.meta.status!==200){
                        this.$message.error('获取商品列表数据失败')
                        return
                    }else {
                        this.total=res.data.data.total
                        this.tableData=res.data.data.goods
                    }
                })
            },
            handleSizeChange(val){
                this.goodsQuery.pagesize=val
                this.getGoodsList()
            },
            handleCurrentChange(val){
                this.goodsQuery.pagenum=val
                this.getGoodsList()
            },
            searchGoods(){
                this.$http.get('goods',{params:this.goodsQuery}).then((res)=>{
                    if (res.data.meta.status!==200){
                        this.$message.error('查询商品数据失败')
                        return
                    }else {
                        this.tableData=res.data.data.goods
                        this.getGoodsList()
                    }
                })
            },
            deleteGoods(row){
                this.$confirm('此操作将永久删除商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete('goods/'+row.goods_id).then((res)=>{
                        if (res.data.meta.status!==200){
                            this.$message.error('删除失败')
                        }else{
                            this.getGoodsList()
                            this.$message.success('删除成功')
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            goGoodsAdd(){
                this.$router.push('/goods/add')
            },
        //    修改商品
            editGoods(row){
                this.$router.push({path:'/goods/edit',query:{id:row.goods_id}})
            }
        },
        created() {
            this.getGoodsList()
        }
    }
</script>

<style scoped>
.el-row{
    display: flex;
}
</style>
